package com.cskaoyan.mall.service.lmpl;

import com.cskaoyan.mall.dao.UserMapper;
import com.cskaoyan.mall.model.bo.user.UserMessageBo;
import com.cskaoyan.mall.model.bo.user.UserPwdChangeBo;
import com.cskaoyan.mall.model.bo.user.UserSignUpMessageBo;
import com.cskaoyan.mall.model.po.User;
import com.cskaoyan.mall.model.vo.user.UserMessageVo;
import com.cskaoyan.mall.service.UserService;
import com.cskaoyan.mall.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @ClassName: UserServiceImpl
 * @Description: TODO
 * @Auther 庄左
 * @Date 2022/6/29 21:41
 * @Version
 **/
public class UserServiceImpl implements UserService {


    //查找所有用户
    @Override
    public List<UserMessageVo> search() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<UserMessageVo> users = mapper.selectUser(null);
        sqlSession.commit();
        sqlSession.close();
        return users;
    }


    // 根据昵称查找用户
    @Override
    public List<UserMessageVo> selectByName(String nickname) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<UserMessageVo> users = mapper.selectUser(new User(nickname));
        sqlSession.commit();
        sqlSession.close();
        return users;
    }

    //删除用户

    @Override
    public void deleteUser(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(id);
        sqlSession.commit();
        sqlSession.close();
    }



//=======================以上为管理系统的方法=========================================//

    //用户登录
    @Override
    public UserMessageVo login(UserMessageVo userMessageVo) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        UserMessageVo userMessageVos = mapper.selectUserLoginInMall(new User(userMessageVo.getPwd(), userMessageVo.getEmail(),null));
        sqlSession.commit();
        sqlSession.close();
        return userMessageVos;
    }

    @Override
    public void insertUser(UserSignUpMessageBo sign) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.insertUser(new User(sign.getAddress(),sign.getPhone(),sign.getNickname(),sign.getRecipient(),null,sign.getPwd(), sign.getEmail()));
        sqlSession.commit();
        sqlSession.close();

    }


    //用户资料页的回显
    @Override
    public UserMessageVo selectUserDataByToken(String token) {


        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        UserMessageVo userMessageVo = mapper.selectUserByNickName(token);
        sqlSession.commit();
        sqlSession.close();
        return userMessageVo;
    }


    //用户修改除密码外的所有资料
    @Override
    public void updateUserData(UserMessageBo bo) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUserData(new User(bo.getAddress(),bo.getPhone(),bo.getNickname(),bo.getRecipient(),bo.getId()));
        sqlSession.commit();
        sqlSession.close();
    }


    public void changePwd(UserPwdChangeBo bo) {

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        mapper.updatePwd(new User(bo.getNewPwd(),null,Integer.valueOf(bo.getId())));
        sqlSession.commit();
        sqlSession.close();


    }
}
